<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>About</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.4.0/axios.min.js" integrity="sha512-uMtXmF28A2Ab/JJO2t/vYhlaa/3ahUOgj1Zf27M5rOo8/+fcTUVH0/E0ll68njmjrLqOBjXM3V9NiPFL5ywWPQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
    <h1>I am About page.</h1>
    <h2 id="element">Waiting for response...</h2>
</body>

<script>
    const get_info = function(skey) {
        axios.get("gitee/getInfo", {
            params: {
                skey,
            }
        }).then(resp => {
            let element = document.getElementById("element")
            console.log(resp.data)
            if (resp.data.state_code !== 200) {
                cookieStore.delete("session_key").then();
                console.log("session_key has be deleted...");
                let inner_html = `
                    <span>Session key has expired!!!</span> </br>
                    <span>Getting a new session key from server...</span>
                `;
                element.innerHTML = inner_html;
                window.onload();
                return;
            }
            let inner_html = `
                <span>avater: </span><img src="${resp.data["avatar_url"]}" style="width:50px; height:50px;"/> <br><br>
                <span>name: ${resp.data["name"]}</span> <br><br>
                <span>url: <a href="${resp.data["html_url"]}">${resp.data["html_url"]}</a></span> <br><br>
                <span>followers: ${resp.data["followers"]}</span> <br><br>
                <span>following: ${resp.data["following"]}</span> <br><br>
                <span>stared: ${resp.data["stared"]}</span> <br><br>
                <span>created_at: ${resp.data["created_at"]}</span> <br><br>
                <span>updated_at: ${resp.data["updated_at"]}</span> <br><br>

            `;
            element.innerHTML = inner_html;

            cookieStore.set("session_key", resp.data["session_key"]);
        });
    }

    window.onload = () => {
        cookieStore.get("session_key").then((s_key) => {
            console.log(s_key);
            if (!s_key) {
                let url = new URL(window.location.toLocaleString());
                let code = url.searchParams.getAll('code')[0];

                axios.get('/gitee/callback', {
                    params: {
                        code
                    },
                }).then((resp) => {
                    console.log(resp);
                    let session_key = resp.data['session_key'];
                    console.log("session_key: " + session_key);
                    get_info(session_key);
                    cookieStore.set('session_key', session_key);
                });
            } else {
                get_info(s_key.value);
            }
        });
    }


</script>
</html>
